package controllers.interfaces.tenant;

import java.util.Map;

import controllers.BaseController;
import net.sf.json.JSONObject;
import play.Logger;
import producers.AccountProducer;
import utils.Constant;
import utils.StringUtil;

public class Accounts extends BaseController {
    /**
     * 账户创建
     */
    public static void create(String id) {
        String sid = Constant.UCPAAS_SID;
        String token = Constant.UCPAAS_TOKEN;
        String nbr = params.get("trunkNos");
        String notifyUrl = params.get("notifyUrl");
        String name = params.get("name");
        JSONObject obj = new JSONObject();
        boolean rcode = false;
        String appId = null;
        String msg = null;
        if (!StringUtil.isBlank("sid") && !StringUtil.isBlank(token)) {
            AccountProducer producer = new AccountProducer(sid, token);
            Map<String, String> result = producer.create(sid, token, name, nbr, notifyUrl, null);
            if ("000000".equals(StringUtil.trim(result.get("rcode")))) {
                rcode = true;
                appId = result.get("appId");
            }
            msg = result.get("msg");
        }
        obj.put("rcode", rcode);
        obj.put("msg",msg);
        obj.put("appId", StringUtil.trim(appId));
        obj.put("id", id);
        Logger.info("[%s]: id<%s>, sid<%s>, token<%s>, trunkNos<%s>, notifyUrl<%s>, name<%s>, rcode<%s>, msg<%s>, appId<%s>", "Create",id,sid,token,nbr,notifyUrl,name,rcode,msg,appId);
        renderJSON(obj);
    }
    
    /**
     * 账户更新
     */
    public static void update(String id) {
        String sid = Constant.UCPAAS_SID;
        String token = Constant.UCPAAS_TOKEN;
        String nbr = params.get("trunkNos");
        String notifyUrl = params.get("notifyUrl");
        String appId = params.get("appId");
        
        JSONObject obj = new JSONObject();
        boolean rcode = false;
        String msg = null;
        if (!StringUtil.isBlank("sid") && !StringUtil.isBlank(token)) {
            AccountProducer producer = new AccountProducer(sid, token);
            Map<String, String> result = producer.update(sid, token, appId, nbr, notifyUrl, null);
            if ("000000".equals(StringUtil.trim(result.get("rcode")))) {
                rcode = true;
                appId = result.get("appId");
            }
            msg = result.get("msg");
            
        }
        obj.put("rcode", rcode);
        obj.put("msg", msg);
        obj.put("appId", appId);
        obj.put("id", id);
        Logger.info("[%s]: id<%s>, sid<%s>, token<%s>, trunkNos<%s>, notifyUrl<%s>, rcode<%s>, msg<%s>, appId<%s>", "Update",id,sid,token,nbr,notifyUrl,rcode,msg,appId);
        renderJSON(obj);
    }

    
    /**
     * 账户删除
     */
    public static void destroy(String id) {
        String sid = Constant.UCPAAS_SID;
        String token = Constant.UCPAAS_TOKEN;
        String appId = params.get("appId");
        String msg = null;
        JSONObject obj = new JSONObject();
        boolean rcode = false;
        if (!StringUtil.isBlank("sid") && !StringUtil.isBlank(token)) {
            AccountProducer producer = new AccountProducer(sid, token);
            Map<String, String> result = producer.destroy(sid, token,appId);
            if ("000000".equals(StringUtil.trim(result.get("rcode")))) {
                rcode = true;
            }
            msg = result.get("msg");
        }
        obj.put("rcode", rcode);
        obj.put("msg", msg);
        obj.put("appId", appId);
        obj.put("id", id);
        Logger.info("[%s]: id<%s>, sid<%s>, token<%s>, rcode<%s>, msg<%s>, appId<%s>", "Destroy",id,sid,token,rcode,msg,appId);
        renderJSON(obj);
    }
}
